home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / x11 / strategy / shanghai.000 / shanghai / shanghai-1.0 / makemap.c < prev    next >
C/C++ Source or Header  |  1995-05-24  |  9KB  |  209 lines

  1. #include <memory.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. #define OUTDATA     "mapdata.c"
  7. #define HEADER      "mapdata.h"
  8.  
  9. #define NIL 255
  10.  
  11. typedef struct {
  12.     unsigned char       index;
  13.     unsigned char       x,y,depth;
  14.     unsigned char       right[3],left[3];
  15.     unsigned char       top[2],bottom[5];
  16.     unsigned char       rdrw[10];
  17.     unsigned char       shd0,shd1;
  18. } Board;
  19.  
  20. static Board board[144];
  21.  
  22. static char def[8][15] = {
  23. " 111111111111  ",
  24. "   12222221    ",
  25. "  1123333211   ",
  26. "111123443211111",
  27. " 111235432111  ",
  28. "  1123333211   ",
  29. "   12222221    ",
  30. " 111111111111  "};
  31.  
  32. static unsigned char map[18][32][6];
  33.  
  34. static int compar(const void *b1,const void *b2)
  35. {
  36.     return(((Board *)b1)->index-((Board *)b2)->index);
  37. }
  38.  
  39. static void makeboard(FILE *out)
  40. {
  41.     int     x,y,i,j,k,count;
  42.     
  43.     memset(board,NIL,sizeof(board));
  44.     memset(map,NIL,sizeof(map));
  45.     for (x = 15, i = 0; x--;)
  46.         for (y = 0; y < 8; y++)
  47.             if (def[y][x] != ' ') {
  48.                 for (count = 0; count < def[y][x]-'0'; count++)
  49.                     map[2*y][2*x][count] = i++; }
  50.     map[7][28][0] = map[6][28][0]; map[6][28][0] = NIL;
  51.     map[7][26][0] = map[6][26][0]; map[6][26][0] = NIL;
  52.     map[7][13][4] = map[8][12][4]; map[8][12][4] = NIL;
  53.     map[7][ 0][0] = map[6][ 0][0]; map[6][ 0][0] = NIL;
  54.     for (x = 30, i = 0; x--;)
  55.         for (y = 0; y < 16; y++)
  56.             for (count = 0; count < 5; count++)
  57.                 if (map[y][x][count] != NIL) {
  58.                     board[i].index = map[y][x][count];
  59.                     board[i].x = x;
  60.                     board[i].y = y;
  61.                     board[i].depth = count;
  62.                     k = 0;
  63.                     if (x) {
  64.                         if (y && map[y-1][x-2][count] != NIL)
  65.                             board[i].left[k++] = map[y-1][x-2][count];
  66.                         if (map[y+1][x-2][count] != NIL)
  67.                             board[i].left[k++] = map[y+1][x-2][count];
  68.                         board[i].left[k++] = map[y][x-2][count]; }
  69.                     k = 0;
  70.                     if (y && map[y-1][x+2][count] != NIL)
  71.                         board[i].right[k++] = map[y-1][x+2][count];
  72.                     if (map[y+1][x+2][count] != NIL)
  73.                         board[i].right[k++] = map[y+1][x+2][count];
  74.                     board[i].right[k++] = map[y][x+2][count];
  75.                     if (count < 4 &&
  76.                         (board[i].top[0] = map[y][x][count+1]) == NIL &&
  77.                         (board[i].top[0] = map[y+1][x+1][count+1]) == NIL &&
  78.                         (board[i].top[0] = map[y+1][x-1][count+1]) == NIL &&
  79.                         (!y || (board[i].top[0] = map[y-1][x+1][count+1]) == NIL) &&
  80.                         y)
  81.                          board[i].top[0] = map[y-1][x-1][count+1];
  82.                     if (count &&
  83.                         (board[i].bottom[0] = map[y][x][count-1]) == NIL) {
  84.                         board[i].bottom[0] = map[y-1][x+1][count-1];
  85.                         board[i].bottom[1] = map[y+1][x+1][count-1];
  86.                         board[i].bottom[2] = map[y-1][x-1][count-1];
  87.                         board[i].bottom[3] = map[y+1][x-1][count-1]; }
  88.                     j = 0;
  89.                     if (y) {
  90.                         if (map[y-2][x+2][0] != NIL)
  91.                             for (k = 0;;k++) if (map[y-2][x+2][k+1]==NIL || k == count) {
  92.                                 board[i].rdrw[j++] = map[y-2][x+2][k];
  93.                                 break; } }
  94.                     if (y) {
  95.                         if (map[y-1][x+2][0] != NIL)
  96.                             for (k = 0;;k++) if (map[y-1][x+2][k+1]==NIL || k == count) {
  97.                                 board[i].rdrw[j++] = map[y-1][x+2][k];
  98.                                 break; } }
  99.                     if (map[y][x+2][0] != NIL)
  100.                         for (k = 0;;k++) if (map[y][x+2][k+1]==NIL || k == count) {
  101.                             board[i].rdrw[j++] = map[y][x+2][k];
  102.                             break; }
  103.                     if (map[y+1][x+2][0] != NIL)
  104.                         for (k = 0;;k++) if (map[y+1][x+2][k+1]==NIL || k == count) {
  105.                             board[i].rdrw[j++] = map[y+1][x+2][k];
  106.                             break; }
  107.                     if (map[y+2][x+2][0] != NIL)
  108.                         for (k = 0;;k++) if (map[y+2][x+2][k+1]==NIL || k == count) {
  109.                             board[i].rdrw[j++] = map[y+2][x+2][k];
  110.                             break; }
  111.                     if (y) {
  112.                         if (map[y-2][x][0] != NIL)
  113.                             for (k = 0;;k++) if (map[y-2][x][k+1]==NIL || k == count) {
  114.                                 board[i].rdrw[j++] = map[y-2][x][k];
  115.                                 break; } }
  116.                     for (k = 0; board[i].bottom[k] != NIL; k++)
  117.                         board[i].rdrw[j++] = board[i].bottom[k];
  118.                     if (map[y+2][x][0] != NIL)
  119.                         for (k = 0;;k++) if (map[y+2][x][k+1]==NIL || k == count) {
  120.                             board[i].rdrw[j++] = map[y+2][x][k];
  121.                             break; }
  122.                     if (x) {
  123.                         if (y) {
  124.                             if (map[y-2][x-2][0] != NIL)
  125.                                 for (k = 0;;k++) if (map[y-2][x-2][k+1]==NIL || k == count) {
  126.                                     board[i].rdrw[j++] = map[y-2][x-2][k];
  127.                                     break; } }
  128.                         if (y) {
  129.                             if (map[y-1][x-2][0] != NIL)
  130.                                 for (k = 0;;k++) if (map[y-2][x-1][k+1]==NIL || k == count) {
  131.                                     board[i].rdrw[j++] = map[y-1][x-2][k];
  132.                                     break; } }
  133.                         if (map[y][x-2][0] != NIL)
  134.                             for (k = 0;;k++) if (map[y][x-2][k+1]==NIL || k == count) {
  135.                                 board[i].rdrw[j++] = map[y][x-2][k];
  136.                                 break; }
  137.                         if (map[y+2][x-2][0] != NIL)
  138.                             for (k = 0;;k++) if (map[y+2][x-2][k+1]==NIL || k == count) {
  139.                                 board[i].rdrw[j++] = map[y+2][x-2][k];
  140.                                 break; }
  141.                         if (map[y+1][x-2][0] != NIL)
  142.                             for (k = 0;;k++) if (map[y+1][x-2][k+1]==NIL || k == count) {
  143.                                 board[i].rdrw[j++] = map[y+1][x-2][k];
  144.                                 break; } }
  145.                     if (y) {
  146.                         board[i].shd0 = map[y-2][x][count];
  147.                         if ((board[i].shd1 = map[y-2][x+2][count]) == NIL)
  148.                             board[i].shd1 = map[y-1][x+2][count]; }
  149.                     i++; }
  150.     if (i != 144)
  151.         fprintf(out,"#error count != 144\n");
  152.     qsort(board,144,sizeof(Board),compar);
  153.     fprintf(out,"\nBoard board[144] = {\n");
  154.     for (i = 0; i < 144; i++)
  155.         fprintf(out,"/* %3d */ "                        /* index    */
  156.                     "{%2d,%2d,%1d,"                     /* x,y,d    */
  157.                     "{%3d,%3d,%3d},{%3d,%3d,%3d},"      /* r[3],l[3]*/
  158.                     "%3d,%3d,"/*"{%3d,%3d,%3d,%3d,%3d},"*//* t[2],b[5]*/
  159.                     /* "{%3d,%3d,%3d,%3d,%3d}," */      /* rd[10]   */
  160.                     /* "{%3d,%3d,%3d,%3d,%3d}," */      /* rd[10]   */
  161.                     "%3d,%3d}%s\n",                     /* shd0,shd1*/
  162.                     board[i].index,
  163.                     board[i].x,board[i].y,board[i].depth,
  164.                     board[i].right[0],board[i].right[1],board[i].right[2],
  165.                     board[i].left[0],board[i].left[1],board[i].left[2],
  166.                     board[i].top[0],/*board[i].top[1],*/
  167.                     board[i].bottom[0],/*board[i].bottom[1],
  168.                     board[i].bottom[2],board[i].bottom[3],
  169.                     board[i].bottom[4],*/
  170.                  /* board[i].rdrw[0],board[i].rdrw[1],
  171.                     board[i].rdrw[2],board[i].rdrw[3],
  172.                     board[i].rdrw[4],board[i].rdrw[5],
  173.                     board[i].rdrw[6],board[i].rdrw[7],
  174.                     board[i].rdrw[8],board[i].rdrw[9], */
  175.                     board[i].shd0,board[i].shd1,
  176.                     i == 143 ? "};" : ",");
  177.     return;
  178. }
  179.  
  180. int     main(void)
  181. {
  182.     FILE    /* *inp, */ *out,*hdr;
  183.     
  184.     if ((hdr = fopen(HEADER,"w")) == NULL ||
  185.         (out = fopen(OUTDATA,"w")) == NULL) {
  186.         return(1); }
  187.     fprintf(out,"#include \"mapdata.h\"\n"
  188.                 "\n");
  189.     makeboard(out);
  190.     fprintf(hdr,"#ifndef __MAP_DATA__\n"
  191.                 "#define __MAP_DATA__\n"
  192.                 "\n"
  193.                 "#define NIL\t255\n"
  194.                 "\n"
  195.                 "typedef struct {\n"
  196.                 "\tunsigned char\t\tx,y,depth;\n"
  197.                 "\tunsigned char\t\tright[3],left[3];\n"
  198.                 "\tunsigned char\t\ttop,bottom;\n"
  199.                 "\tunsigned char\t\tshd0,shd1;\n"
  200.                 "} Board;\n"
  201.                 "\n"
  202.                 "extern Board board[144];\n"
  203.                 "\n"
  204.                 "#endif\n");
  205.     fclose(out);
  206.     fclose(hdr);
  207.     return(0);
  208. }
  209.